﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Project
{
    public partial class studentProfile_ : System.Web.UI.Page
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["StudentAdvisorString"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command_profile = null;
        SqlCommand command_update = null;
        SqlDataReader dataReader = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userID"] == null)
                Response.Redirect("Default.aspx");
            else
                lbl_username.Text = "Welcome, " + Session["userID"].ToString();

#region Navid_Code
            //My Code If u find any problem in this code let me know i will change it
            try
            {
                connection.Open();
                String lastLoggedIn = "select lastlogin from userlogs where username=@name";
                SqlCommand lastLoggedCmd = new SqlCommand(lastLoggedIn, connection);

                SqlParameter para4 = new SqlParameter();
                para4.ParameterName = "@name";
                para4.Value = Session["userID"].ToString();
                lastLoggedCmd.Parameters.Add(para4);

                dataReader = lastLoggedCmd.ExecuteReader();
                DateTime sd = new DateTime();
                while (dataReader.Read())
                {
                    if (dataReader.IsDBNull(0))
                    {
                        lbl_lastlogin.Text = "You Last Logged In: This is the first time you logged in";
                    }
                    else
                    {
                        sd = (DateTime)dataReader[0];
                    }
                }
                dataReader.Close();
                connection.Close();
                String lastloginString = sd.ToString();

                lbl_lastlogin.Text = " You Last Logged In: " + lastloginString;

            }
            catch (SqlException ex)
            {

            }

#endregion Navid_Code 

            string profile_string = "select * from tblStudentInfo where StudentId='" + Session["userID"].ToString() + "'";
            command_profile = new SqlCommand(profile_string, connection);
            try
            {
                connection.Open();
                dataReader = command_profile.ExecuteReader();
                dataReader.Read();
                txt_firstName.Text = dataReader["StudentFirstName"].ToString();
                txt_lastName.Text = dataReader["StudentLastName"].ToString();
                txt_programCode.Text = dataReader["ProgramCode"].ToString();
                txt_email.Text = dataReader["EmailPrimary"].ToString();
                txt_emailS.Text = dataReader["EmailSecondary"].ToString();
                txt_cellNumber.Text = dataReader["CellNumber"].ToString();

            }
            catch (SqlException ex)
            {
            }
            finally
            {
                if (!dataReader.IsClosed)
                {
                    dataReader.Close();
                    connection.Close();
                }
            }
            txt_firstName.Enabled = false;
            txt_lastName.Enabled = false;
            txt_programCode.Enabled = false;
            txt_email.Enabled = false;
            txt_emailS.Enabled = false;
            txt_cellNumber.Enabled = false;
            dl_international.Enabled = false;

        }

        protected void btn_logout_Click(object sender, EventArgs e)
        {
#region Navid_code
            //My Code If u find any problem in this code let me know i will change it
            connection.Open();
            String updateLogOutTime = "update userlogs set userlogout=GETDATE(), lastlogin = userlogin where username=@un";
            SqlCommand LogOut = new SqlCommand(updateLogOutTime, connection);
            SqlParameter para4 = new SqlParameter();
            para4.ParameterName = "@un";
            para4.Value = Session["userID"].ToString();
            LogOut.Parameters.Add(para4);

            LogOut.ExecuteNonQuery();

            connection.Close();

#endregion Navid_code

            Session["userID"] = null;
            Session["userType"] = null;
            Response.Redirect("Default.aspx");
        }

        //protected void btn_edit_update_Click(object sender, EventArgs e)
        //{
        //    if (btn_edit_update.Text == "Edit")
        //    {
        //        txt_firstName.Enabled = true;
        //        txt_lastName.Enabled = true;
        //        txt_programCode.Enabled = true;
        //        txt_email.Enabled = true;
        //        txt_emailS.Enabled = true;
        //        txt_cellNumber.Enabled = true;
        //        dl_international.Enabled = true;
        //        btn_edit_update.Text = "Update";
        //    }
        //    else
        //    {
        //        string update_string = "update tblStudentInfo set StudentFirstName= @studentFirstName, StudentLastName=@studentLastName, ProgramCode=@programCode, EmailPrimary=@emailPrimary, EmailSecondary=@emailSecondary, CellNumber=@cellNumber where StudentId=@studentId";
        //        command_update = new SqlCommand(update_string, connection);

        //        command_update.Parameters.AddWithValue("@studentFirstName", txt_firstName.Text);
        //        command_update.Parameters.AddWithValue("@studentLastName", txt_lastName.Text);
        //        command_update.Parameters.AddWithValue("@programCode", txt_programCode.Text);
        //        command_update.Parameters.AddWithValue("@emailPrimary", txt_email.Text);
        //        command_update.Parameters.AddWithValue("@emailSecondary", txt_emailS.Text);
        //        command_update.Parameters.AddWithValue("@cellNumber", txt_cellNumber.Text);
        //        command_update.Parameters.AddWithValue("@studentId", Session["userID"].ToString());

        //        try
        //        {
        //            connection.Open();
        //            command_update.ExecuteNonQuery();
        //            connection.Close();
        //            lbl_update.Text = "Your profile has been updated...";
        //        }
        //        catch (SqlException ex)
        //        {
        //            lbl_update.Text = ex.ToString();
        //        }
        //        finally
        //        {

        //        }
        //        btn_edit_update.Text = "Edit";
        //    }
       // }

        protected void btn_appointment_Click(object sender, EventArgs e)
        {
            Response.Redirect("appointment.aspx");
        }
    }
}